package com.itheima.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/*
*   映射工具类
* */
public class MapperUtils {

    static SqlSessionFactory factory = null;

    //静态代码块 在类加载的时候运行
    static {
        try {
            //读取核心配置文件
            InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
            //构建工厂对象
            factory = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //获取sqlSession
    public static SqlSession getSqlSession(){
        //获取sqlSession对象
        SqlSession sqlSession = factory.openSession();
        return sqlSession;
    }
}
